package cn.magic.org.service.impl;

import cn.magic.basic.exception.BusinessException;
import cn.magic.basic.utils.BaiduAuditUtils;
import cn.magic.org.domain.Employee;
import cn.magic.org.domain.Shop;
import cn.magic.org.domain.ShopAuditLog;
import cn.magic.org.mapper.EmployeeMapper;
import cn.magic.org.mapper.ShopAuditLogMapper;
import cn.magic.org.mapper.ShopMapper;
import cn.magic.org.service.IShopService;
import cn.magic.basic.service.impl.BaseServiceImpl;
import cn.magic.org.vo.ShopEchartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pc
 * @since 2022-07-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private EmployeeServiceImpl employeeService;

    // 注入邮件对象
    @Autowired
    private JavaMailSender javaMailSender;

    /** 
    * @Title: settlement
    * @Description: 店铺入住方法
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/1 17:00  
    * @Parameters: [shop]
    * @Return void
    */
    @Override
    public void settlement(Shop shop) {
        // 将二次，密码抽取出来
        Employee admin = shop.getAdmin();

        // 判断参数是否为空
        if (StringUtils.isEmpty(shop.getName()) ||
            StringUtils.isEmpty(shop.getTel()) ||
            StringUtils.isEmpty(shop.getAddress()) ||
            StringUtils.isEmpty(shop.getAdmin().getUsername()) ||
            StringUtils.isEmpty(shop.getAdmin().getPassword()) ||
            StringUtils.isEmpty(shop.getAdmin().getComfirmPassword()) ||
            StringUtils.isEmpty(shop.getAdmin().getPhone()) ||
            StringUtils.isEmpty(shop.getAdmin().getEmail()) ){
            throw new BusinessException("参数不能为空请重新输入");
        }

        // 判断两此密码是否一致 第一次输入的密码在前比较
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致，请重新输入");
        }

        // 我们需要验证店铺的名称是否合规
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("请规范输入店铺名称");
        }
        // 我们需要验证logo图片是否合规
        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BusinessException("请上传正确的logo");
        }


        // 判断店铺是否存在
        Shop loadShopTmp = shopMapper.loadByName(shop.getName());
        if (loadShopTmp != null){
            throw new BusinessException("店铺已注册，请跟换店铺信息");
        }

        // 查询管理员已经注册过，并且还绑定了店铺，这种情况我们认为不符合我们的业务，抛出异常
        Employee empTmp = employeeMapper.loadByUserName(admin.getUsername());

//        if (empTmp != null && empTmp.getShopId() != null){
//            throw new BusinessException("该管理员已经绑定店铺，请跟换其他管理员");
//        }
        // 判断管理员是否绑定店铺逻辑
        if (empTmp != null && shopMapper.loadByAdminId(empTmp.getId()) == null){
            throw new BusinessException("当前管理员已有店铺，请更改管理员！");
        }


        // 管理员注册了没绑定/管理员没注册
        if(empTmp == null ){
            employeeService.save(admin);
            empTmp=admin;
        }

        // 如果到这儿了证明管理员已经注册成功了
        // 将管理员的主键设置给店铺的外键
        shop.setAdminId(empTmp.getId());
        // 保存店铺，一定要返回主键
        shopMapper.save(shop);
        // 将店铺的主键设置到管理员的外键
        empTmp.setShopId(shop.getId());
        // 修改员工数据
        employeeService.update(empTmp);


    }

    /**
    * @Title: pass
    * @Description: 通过店铺
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/2 16:11
    * @Parameters: [shop]
    * @Return void
    */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        // 获取操作日志的shopid，转换为long类型
        long shopId = shopAuditLog.getShopId().longValue();
        // 修改店铺的状态为驳回
        // 将shopid传入通过id查找中
        Shop shop = shopMapper.loadById(shopId);

        shop.setState(2);
        // 修改
        shopMapper.update(shop);

        // 2保存日志信息
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);

        // 发送激活邮件给用户
        // 1.准备发送邮箱的配置对象的相关信息
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("604707679@qq.com");
            helper.setSubject("宠物乐园店铺入住结果");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>您的店铺入住平台消息已经审核通过，请点击下方的链接激活店铺</h1>"+
                    "店铺激活链接: <a href='http://localhost:8080/shop/activation/"+shopAuditLog.getShopId()+"'>点我激活店铺</a>",true);
            // 5.设置收件人
            helper.setTo("604707679@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
    * @Title: reject
    * @Description: 驳回店铺
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/2 16:11
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        // 获取操作日志的shopid，转换为long类型
        long shopId = shopAuditLog.getShopId().longValue();
        // 修改店铺的状态为驳回
        // 将shopid传入通过id查找中
        Shop shop = shopMapper.loadById(shopId);

        shop.setState(-1);
        // 修改
        shopMapper.update(shop);

        // 2保存日志信息
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);

        // 发送修改店铺信息邮件给用户
        // 1.创建复杂邮件对象
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("604707679@qq.com");
            helper.setSubject("宠物乐园店铺入住结果");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>您的店铺入住平台消息已经审核，但是内容不合规被驳回，请修改后，再次申请</h1>"+
                    "信息修改链接:<a href='http://localhost:8081/#/shopRegisterModify?"+shop.getId()+"'>点我修改店铺信息</a>",true);
            // 5.设置收件人
            helper.setTo("604707679@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    /**
    * @Title: loadEcharts
    * @Description: 查询店铺报表数据
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/2 19:24
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    */
    @Override
    public Map<String, Object> loadEcharts() {
        List<ShopEchartVo> shopEchartVos = shopMapper.loadEcharts();

        List<Integer> collects = shopEchartVos.stream().map(ShopEchartVo::getCount).collect(Collectors.toList());
       /* List<Integer> states = shopEchartVos.stream().map(ShopEchartVo::getState).collect(Collectors.toList());*/
        HashMap<String, Object> map = new HashMap<>();

        ArrayList<String> states = new ArrayList<>();
        for (ShopEchartVo shopEchartVo : shopEchartVos) {
            Integer state = shopEchartVo.getState();
            if (state == 1){
                states.add("审核通过");
            } else if (state == -1){
                states.add("驳回");
            } else if (state == 2){
                states.add("待激活");
            } else if (state == -2){
                states.add("拒绝");
            } else {
                states.add("待审核");
            }
        }

        map.put("x",states);
        map.put("y",collects);
        return map;
    }

    /**
    * @Title: repulse
    * @Description: 拒绝店铺
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/3 11:46
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void repulse(ShopAuditLog shopAuditLog) {
        // 获取操作日志的shopid，转换为long类型
        long shopId = shopAuditLog.getShopId().longValue();
        // 修改店铺的状态为驳回
        // 将shopid传入通过id查找中
        Shop shop = shopMapper.loadById(shopId);

        shop.setState(-2);
        // 修改
        shopMapper.update(shop);

        // 2保存日志信息
        shopAuditLog.setAuditId(325);
        shopAuditLogMapper.save(shopAuditLog);

        // 1.创建复杂邮件对象
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("604707679@qq.com");
            helper.setSubject("宠物乐园店铺入住结果");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>您的店铺入住平台消息经过审核，因涉及违规，已拒绝</h1>",true);
            // 5.设置收件人
            helper.setTo("604707679@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    /**
    * @Title: batchSave
    * @Description: 批量导入文件
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/3 18:31
    * @Parameters: [shops]
    * @Return void
    */
    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }

    /**
    * @Title: activation
    * @Description: 店铺激活
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/3 23:47
    * @Parameters: [id]
    * @Return void
    */
    @Override
    public void activation(Long id) {
        // 通过id查询
        Shop shop = shopMapper.loadById(id);
        // 因为审核通过要改变状态 为 1已审核状态
        shop.setState(1);
        // 修改
        shopMapper.update(shop);

    }
}
